<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Spaghetti Coders</title>
<link rel="stylesheet" type="text/css" href="includes/style.css">
</head>

<body>

<?php

$username = $_SERVER['PHP_AUTH_USER'];

include "includes/lib.php";
include "includes/header.php"; 
include "includes/nav.php";



echo '<div id="wrapper">';
echo '<div id="content">';


if (isset($_GET["action"]) == false) {

	echo 'ERROR: No action defined.';
}

else {

$action = $_GET["action"];
	
	switch($_GET["action"]) {

	case "list_checkouts":
		echo "<h3> Documents Checked Out By This User </h3><br />";
		list_checkouts_for_user($username);
		break;

	case "manage_perms":
		echo "<h3> Modify Document Permissions </h3><br />";
		manage_perms();
		break;



	case "discard_checkout":
		if (isset($_GET["doc_id"]) == false) {
			echo "ERROR: No doc_id defined.";
		}
		else {
			$doc_id = mysql_real_escape_string($_GET["doc_id"]);
			discard_checkout($username, $doc_id);
			echo "<h3> Documents Checked Out By This User </h3><br />";
			list_checkouts_for_user($username);
		}
		break;

	case "checkout":
		//if user has permission
		if (isset($_GET['doc_id'])){
			$doc_id = mysql_real_escape_string($_GET['doc_id']);
			checkout_document($doc_id);
			echo "<h3> All Documents </h3> <br />";
			list_all_documents();
		}
		else {
			echo "Error: No Document ID provided.";
		}
		$referer=$_SERVER['HTTP_REFERER'];
		break;

	case "browse":
		echo "<h3> All Documents </h3> <br />";
		list_all_documents();
		break;

	case "view":
		if (isset($_GET["doc_id"]) == false) {
			echo "ERROR: No doc_id defined.";
		}
		else {
			$doc_id = mysql_real_escape_string($_GET["doc_id"]);
			view_doc($doc_id);
		}
		break;

	case "viewperms":
		if (isset($_GET["doc_id"]) == false) {
			echo "ERROR: No doc_id defined.";
		}
		else {
			$doc_id = mysql_real_escape_string($_GET["doc_id"]);
			view_perms($doc_id);
		}
		break;

	case "setperms":
		if (isset($_GET["doc_id"]) && isset($_GET["user_id"]) ) {
			$doc_id = mysql_real_escape_string($_GET["doc_id"]);
			$user_id= mysql_real_escape_string($_GET["user_id"]);
			$has_read = ( isset($_POST["has_read"]) ? 1 : 0);
			$has_write = ( isset($_POST["has_write"]) ? 1 : 0);

			set_perms($user_id,$doc_id,$has_read,$has_write);
			echo "<h3> Modify Document Permissions </h3>";
			manage_perms();

		}
		else {
			echo "Error: Unable to set permissions.  No document id provided.";
		}
		
		break;


	case "create_form":
                        echo '<h3> Create New Document </h3><br />';
                        echo '<form action ="documents.php?action=create_document" method="post">';
                        echo 'Document Name: <input type="text" name="doc_name"><br />';
                        echo 'Description: <input type="text" name="doc_desc"><br />';
                        echo '<input type="Submit" value="Submit">';
                        echo '</form>';
                break;

	case "check_in_form":

		if (isset($_GET["doc_id"])){

			$doc_id = mysql_real_escape_string($_GET["doc_id"]);

                        echo '<h3> Document Check-in </h3><br />';
                        echo '<form action ="documents.php?action=check_in_document&doc_id='.$doc_id.'" method="post">';
                        echo 'Description: <input type="text" name="ver_desc"><br />';
			echo 'File: <input type="file" name="ver_file"><br />';
			echo 'Software Package'; package_dropdown(); echo "<br />";
                        echo '<input type="Submit" value="Submit">';
                        echo '</form>';
			}
                break;


case "check_in_document":
                if (isset($_POST['ver_desc']) and isset($_POST['ver_file']) and isset($_GET['doc_id']) && isset($_POST['package']) ){
                        $doc_id = mysql_real_escape_string($_GET['doc_id']);
                        $ver_desc = mysql_real_escape_string($_POST['ver_desc']);
			$ver_file = mysql_real_escape_string($_POST['ver_file']);
			$pkg_id = mysql_real_escape_string($_POST['package']);
			check_in_document($doc_id,$ver_desc,$pkg_id);
                }
                else {
                        echo "Error:  Unable to create document.  Insufficient information.";
                }
                break;




	case "create_document":
                if (isset($_POST['doc_name']) and isset($_POST['doc_desc'])) {
                        $doc_name = mysql_real_escape_string($_POST['doc_name']);
                        $doc_desc = mysql_real_escape_string($_POST['doc_desc']);
			create_document($doc_name,$doc_desc);
			echo "<h3> All Documents </h3><br />";
			list_all_documents();
                }
                else {
                        echo "Error:  Unable to create document.  Insufficient information.";
                }
                break;

        default:
                echo "No such action";
                break;

	default:
		echo "No such action";
		break;
	}
}
?>	
</div>
</body>
</html>

